package com.sinodata.bsm.cicp.finder.tlq;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.CommandReader;

/**
 * 
 * <p>
 * Description: TLQQueue 的发现程序，发现队列名称
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-26 AM 10:24:49   liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class TLQQueueFinder extends Finder {

    private static final Logger logger = Logger.getLogger(TLQQueueFinder.class);

    public TLQQueueFinder() {

    }

    /**
     * 发现程序
     * 
     * @param pResValue  父资源的信息
     * @param pattrVals  父资源属性
     * @param attributes 资源自带的属性
     * @return 资源列表
     */
    /*@Override
    public List<List<Serializable>> find(ResValue pResValue, ResAttributeValValue[] pattrVals, ResAttributeValue[] attributes) {
        String shell = "looknum 1";
        BufferedReader reader = null;
        try {
            reader = CollectorUtil.getReader(shell, host);
            return this.parseReader(reader, pattrVals, attributes);
        } catch (IOException e) {
            logger.error("Failed to auto find TLQQueue ", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("Failed to close IO resource ", e);
                }
            }
        }
        return new ArrayList<List<Serializable>>();
    }*/

    /**
     * 解析命令响应结果
     * 
     * @param reader 命令响应结果
     * @param pattrVals  接口父资源 节点的属性信息
     * @param attributes 资源自带的属性
     * @return 资源列表
     * @throws IOException
     */
    /* private List<List<Serializable>> parseReader(BufferedReader reader, ResAttributeValValue[] pattrVals, ResAttributeValue[] attributes) throws IOException {
         List<List<Serializable>> tlqResources = null;
         // 匹配：====================xxx====================
         Pattern typePatn = Pattern.compile("\\s*=+([^=]+)=+\\s*");
         // 匹配： 2 S_3619_1 0
         Pattern codepatn = Pattern.compile("^\\s*\\d+\\s+(\\S+)\\s*(\\d+)$");
         String tmp = null;
         List<Serializable> resAndAttr = null;
         Matcher typeMach = null;
         Matcher codeMach = null;
         String type = null;
         tlqResources = new ArrayList<List<Serializable>>();
         while ((tmp = reader.readLine()) != null) {
             typeMach = typePatn.matcher(tmp);
             codeMach = codepatn.matcher(tmp);
             if (!typeMach.matches() && !codeMach.matches()) {
                 continue;
             } else if (codeMach.matches() && tlqResources != null) {
                 resAndAttr = new ArrayList<Serializable>();
                 String busCode = getCode(codepatn, tmp);
                 resAndAttr.add(findRes(busCode));
                 resAndAttr.addAll(findAttr(attributes, busCode, type));
                 tlqResources.add(resAndAttr);
             } else if (typeMach.matches()) {// 获取队列类型
                 type = typeMach.group(1);
             }
         }
         return tlqResources;
     }*/

    /**
     * 获取匹配的组的值
     * 
     * @param codePatn Pattern
     * @param tmp 要匹配的字符串
     * @return 匹配的结果
     */
    private String getCode(Pattern codePatn, String tmp) {
        Matcher codeMach = codePatn.matcher(tmp);
        codeMach.matches();
        return codeMach.group(1);
    }

    /**
     * 创建ResValue
     * 
     * @param busCode 资源名称
     * @return ResValue
     */
    /* private ResValue findRes(String busCode) {
         ResValue resValue = new ResValue();
         resValue.setIp(host);
         resValue.setName(busCode);
         resValue.setInternalId(busCode);
         resValue.setDescript("Tonglink Queue");
         return resValue;
     }*/

    /**
     * 创建资源的属性
     * 
     * @param attributes
     * @param i  属性数组的角标
     * @param busCode 设置给属性的值
     * @return
     */
    /*private List<ResAttributeValValue> findAttr(ResAttributeValue[] attributes, String busCode, String type) {
        List<ResAttributeValValue> attValueArray = new ArrayList<ResAttributeValValue>();
        ResAttributeValValue nameAttr = new ResAttributeValValue();
        nameAttr.setResAttributeId(attributes[0].getId());// name
        nameAttr.setAttrValue(busCode);
        attValueArray.add(nameAttr);
        ResAttributeValValue typeAttr = new ResAttributeValValue();
        typeAttr.setResAttributeId(attributes[1].getId());// name
        typeAttr.setAttrValue(type);
        attValueArray.add(typeAttr);
        return attValueArray;
    }*/

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        String shell = "looknum 1";
        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, shell, 0, 0);
            return parseReader(reader);
        } catch (IOException e) {
            logger.error("Failed to auto find TLQQueue ", e);
            return null;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("Failed to close IO resource ", e);
                }
            }
        }
    }

    private List<String[]> parseReader(BufferedReader reader) throws IOException {
        List<String[]> list = new ArrayList<String[]>();
        // 匹配：====================xxx====================
        Pattern typePatn = Pattern.compile("\\s*=+([^=]+)=+\\s*");
        // 匹配： 2 S_3619_1 0
        Pattern codepatn = Pattern.compile("^\\s*\\d+\\s+(\\S+)\\s*(\\d+)$");
        String tmp = null;
        Matcher typeMach = null;
        Matcher codeMach = null;
        String type = null;
        while ((tmp = reader.readLine()) != null) {
            typeMach = typePatn.matcher(tmp);
            codeMach = codepatn.matcher(tmp);
            if (!typeMach.matches() && !codeMach.matches()) {
                continue;
            } else if (codeMach.matches() && list != null) {
                String busCode = getCode(codepatn, tmp);
                String[] info = new String[5];
                list.add(info);
                info[0] = busCode;
                info[1] = busCode;
                info[2] = "Tonglink Queue";
                info[3] = busCode; //队列名
                info[4] = type; //队列类型
            } else if (typeMach.matches()) {// 获取队列类型
                type = typeMach.group(1);
            }
        }
        return list;
    }
}